import React, {PureComponent} from "react";
import {Table} from "antd";
import {connect} from "react-redux";
import {paginationProps, tableProps} from "../../lib/ui";
import {getUserList, query} from "./actions";
import {getLastFilter, getPrincipal} from "../../lib/identity";

const LAST_FILTER = "log";

class List extends PureComponent {

    onPageChange = (page, pageSize) => {
        const {dispatch, filter} = this.props;
        dispatch(query({userId: getPrincipal().id, ...filter, page, pageSize}));
    };

    componentWillMount() {
        const {dispatch} = this.props;
        let fields = getLastFilter(LAST_FILTER);
        if (fields === null || fields === undefined) {
            fields = {};
        }
        dispatch(query({userId: getPrincipal().id, ...fields}));
        dispatch(getUserList(getPrincipal().id));
    }

    render() {
        const columns = [
            {
                title: "操作时间",
                dataIndex: "createTime",
                width: "180px",
            }, {
                title: "操作人",
                dataIndex: "userName",
                width: "200px"
            }, {
                title: "内容",
                dataIndex: "content",
                ellipsis: true
            }, {
                title: "目标参数",
                dataIndex: "targetId"
            }
        ];
        const {
            page,
            pageSize,
            totalElements,
            dataSource,
            loading
        } = this.props;

        const tablePagination = {
            ...paginationProps,
            total: totalElements,
            current: page,
            pageSize: pageSize,
            onShowSizeChange: (current, newSize) =>
                this.onPageChange && this.onPageChange(1, newSize),
            onChange: this.onPageChange
        };
        return (
            <Table
                {...tableProps}
                columns={columns}
                scroll={{x: 1500, y: 'calc(100vh - 350px)'}}
                pagination={tablePagination}
                dataSource={dataSource}
                loading={loading}
            />
        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.log.list
    };
};

export default connect(mapStateToProps)(List);
